package com.youseu.taoliving.commodity.service.impl;

import com.youseu.taoliving.commodity.dao.SpuInfoDao;
import com.youseu.taoliving.commodity.entity.SpuInfoEntity;
import com.youseu.taoliving.commodity.vo.SearchResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youseu.common.utils.PageUtils;
import com.youseu.common.utils.Query;

import com.youseu.taoliving.commodity.dao.SkuInfoDao;
import com.youseu.taoliving.commodity.entity.SkuInfoEntity;
import com.youseu.taoliving.commodity.service.SkuInfoService;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        //查询条件
        if (StringUtils.isNotBlank(key)) {
            wrapper.and(item -> {
                item.eq("sku_id", key).or().like("sku_name", key);
            });
        }
        //品牌id 前端传0过来表示查询时不带该条件
        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotBlank(brandId) && !"0".equals(brandId)) {
            wrapper.eq("brand_id", brandId);
        }
        //分类id
        String catelogId = (String) params.get("catelogId");
        if (StringUtils.isNotBlank(catelogId) && !"0".equals(catelogId)) {
            wrapper.eq("catalog_id", catelogId);
        }
        //价格区间 后端自动将前端的price中的min max放进map
        String max = (String) params.get("max");
        String min = (String) params.get("min");

        if (StringUtils.isNotBlank(min)) {
            wrapper.ge("price", min);
        }
        //最大值大于0时带入条件
        if (StringUtils.isNotBlank(max)) {
            BigDecimal maxPrice = new BigDecimal(max);
            if (maxPrice.compareTo(new BigDecimal("0")) > 0) {
                wrapper.le("price", max);
            }

        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Resource
    private SpuInfoDao spuInfoDao;

    @Override
    public SearchResult queryPageByUserCondition(Map<String, Object> params) {
        //取出上架的spu信息
        List<SpuInfoEntity> spuInfoEntities = spuInfoDao.selectList(new QueryWrapper<SpuInfoEntity>().eq("publish_status", 1));
        List<Long> spuIds = spuInfoEntities.stream().map(item -> item.getId()).collect(Collectors.toList());

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        if (spuIds.size() > 0) {
            wrapper.in("spu_id", spuIds);
        }else{
            //没有任何上架的家居商品, 构造一个结果返回，注意不能直接返回null 因为前台要解析的.
            SearchResult searchResult = new SearchResult();
            searchResult.setCommodity(new ArrayList<>());
            searchResult.setPageNum(1);
            searchResult.setTotal(0L);
            searchResult.setTotalPages(0);
            searchResult.setPageNavs(new ArrayList<>());
            searchResult.setKeyword(params.get("keyword") == null ? "" : params.get("keyword").toString());
            return searchResult;
        }

        String keyword = (String) params.get("keyword");
        if (StringUtils.isNotBlank(keyword)) {
            wrapper.and(item -> wrapper.eq("sku_id", keyword).or().like("sku_name", keyword));
        }
        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotBlank(brandId) && "0".equals(brandId)) {
            wrapper.eq("brand_id", brandId);
        }
        String catalog3Id = (String) params.get("catalog3Id");
        if (StringUtils.isNotBlank(catalog3Id) && "0".equals(catalog3Id)) {
            wrapper.eq("catalog_id", catalog3Id);
        }
        IPage<SkuInfoEntity> page1 = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );
        PageUtils page = new PageUtils(page1);

        SearchResult searchResult = new SearchResult();
        searchResult.setCommodity((List<SkuInfoEntity>) page.getList());
        searchResult.setPageNum(page.getCurrPage());
        searchResult.setTotal((long)page.getTotalCount());
        int totalPage = page.getTotalPage();
        searchResult.setTotalPages(totalPage);
        List<Integer> pageNavs = new ArrayList<>();
        for (int i = 0; i < totalPage; i++) {
            pageNavs.add(i);
        }
        searchResult.setPageNavs(pageNavs);
        searchResult.setKeyword(params.get("keyword") == null ? "" : params.get("keyword").toString());
        return searchResult;
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.save(skuInfoEntity);
    }

}